#include "Kwic.h"

vector<string> Kwic::Run(string filePath) {
	Input(filePath);
	CircularShift();
	Alphabetizer();
	vector<string> ret = Output();
	return ret;
}

void Kwic::Input(string filePath) {
	ifstream finput;
	finput.open(filePath.data());
	assert(finput.is_open());


	string s;
	while(getline(finput,s)) {
		Utils::Strip(s);
		charectors.push_back(s);
	}
	finput.close();
}

void Kwic::CircularShift() {
	int len = charectors.size();
	for(int i = 0; i < len; ++i) {
		string str = charectors[i];
		Utils::Strip(str);
		
		vector<string> words = Utils::Split(str, ' ');

		string temp_str;
		int size = words.size();
		for(int j = 0; j < size - 1; ++j) {
			words.push_back(words[0]);
			words.erase(words.begin());

			string new_line = "";
			for(int k = 0; k < words.size(); ++k) {
				new_line += (words[k] + " ");	
			}
			charectors.push_back(new_line);
		}
	}
}

void Kwic::Alphabetizer() {
	sort(charectors.begin(), charectors.end());
}

vector<string> Kwic::Output() {
	return charectors;
}
